package me.yan.controller.admin;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import me.yan.constant.ErrorConstant;
import me.yan.constant.Types;
import me.yan.constant.WebConst;
import me.yan.controller.BaseController;
import me.yan.exception.BusinessException;
import me.yan.pojo.AttAchDomain;
import me.yan.pojo.UserDomain;
import me.yan.utils.TaleUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Tag(name="附件相关接口")
@Controller
@Slf4j
@RequestMapping("admin/attach")
public class AttachController extends BaseController {
    private final static Logger LOGGER=log;
    public static final String CLASSPATH = TaleUtils.getUplodFilePath();

    @Operation(summary = "markdown文件上传")
    @PostMapping("/uploadfile")
    public void fileUpLoadToTencentCloud(HttpServletRequest request,
                                         HttpServletResponse response,
                                         @Parameter(name = "editormd-image-file", description = "文件数组", required = true)
                                         @RequestParam(name = "editormd-image-file", required = true)
                                         MultipartFile file){
        //文件上传
        try {
            System.out.println("enter file");
            request.setCharacterEncoding( "utf-8" );
            response.setHeader( "Content-Type" , "text/html" );
            String originalFilename = file.getOriginalFilename();
            String fileName = TaleUtils.getFileKey(file.getOriginalFilename()).replaceFirst("/","");


            qiniuCloudService.upload(file, fileName);  //七牛云上传文件
            //向数据库里添加数据信息
            AttAchDomain attAch = new AttAchDomain();
            HttpSession session = request.getSession();
            UserDomain sessionUser = (UserDomain) session.getAttribute(WebConst.LOGIN_SESSION_KEY);
            attAch.setAuthorId(sessionUser.getUid());   //谁创建的图片
            attAch.setFtype(TaleUtils.isImage(file.getInputStream()) ? Types.IMAGE.getType() : Types.FILE.getType());
            attAch.setFname(fileName);
            String baseUrl = qiniuCloudService.QINIU_UPLOAD_SITE.endsWith("/") ? qiniuCloudService.QINIU_UPLOAD_SITE : qiniuCloudService.QINIU_UPLOAD_SITE + "/";
            attAch.setFkey(baseUrl + fileName);
            attAchService.addAttAch(attAch);
            response.getWriter().write( "{\"success\": 1, \"message\":\"上传成功\",\"url\":\"" + attAch.getFkey() + "\"}" );
        } catch (IOException e) {
            e.printStackTrace();
            try {
                response.getWriter().write( "{\"success\":0}" );
            } catch (IOException e1) {
                throw BusinessException.withErrorCode(ErrorConstant.Att.UPLOAD_FILE_FAIL)
                        .withErrorMessageArguments(e.getMessage());
            }
            throw BusinessException.withErrorCode(ErrorConstant.Att.UPLOAD_FILE_FAIL)
                    .withErrorMessageArguments(e.getMessage());
        }
    }
}
